package com.example.fw;


import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.example.tests.ContactData;
import com.example.utils.SortedListOf;


public class ContactHelper extends WebDriverHelperBase {
        
        public static boolean CREATION = true;
        public static boolean MODIFICATION = false;


        public ContactHelper(ApplicationManager manager) {
                super(manager);
        }

        public SortedListOf<ContactData> getUiContacts() {
            manager.navigateTo().mainPage();
            SortedListOf<ContactData> contacts = new SortedListOf<ContactData>(); 
            List<WebElement> rows = driver.findElements(By.xpath("//tr[@name='entry']"));
            
            for (int i=0;i<rows.size();i++) {
                    ContactData contact = new ContactData()
                            .withFirstname(rows.get(i).findElement(By.xpath("./td[3]")).getText())
                            .withLastname(rows.get(i).findElement(By.xpath("./td[2]")).getText());
                            contacts.add(contact);
            }       
            
            return contacts;
        }
              
      


        public ContactHelper createContact(ContactData contact) {
                manager.navigateTo().mainPage(); 
                addNewContact();
                fillContactForm(contact, MODIFICATION);
                submitContactCreation();
                returnToHomePage();
                return this;
                
        }
        public ContactHelper modifyContact(int index, ContactData contact) {
        manager.navigateTo().mainPage(); 
        initContactModification(index);
        fillContactForm(contact, MODIFICATION);
        updateContact();
        returnToHomePage();
        return this;
        }


    	public ContactHelper deleteContact(int index) {
                manager.navigateTo().mainPage(); 
                initContactModification(index);
                deleteContact();
                returnToHomePage();
        return this;
    	}
        
        //-----------------------------------------------------------------------------
        
        public ContactHelper addNewContact() {
                click(By.linkText("add new"));
                return this;
        }


        public ContactHelper fillContactForm(ContactData contact, boolean formType) {
                type(By.name("firstname"), contact.getFirstname());
                type(By.name("lastname"), contact.getLastname());
                type(By.name("address"), contact.getAddress1());
                type(By.name("home"), contact.getHome1());
                type(By.name("mobile"), contact.getMobile1());
                type(By.name("work"), contact.getWork1());
                type(By.name("email"), contact.getEmail());
                type(By.name("email2"), contact.getEmail2());
                selectByText(By.name("bday"), contact.getBday());
                selectByText(By.name("bmonth"), contact.getBmonth());
                type(By.name("byear"), contact.getByear());
                if (formType == CREATION) {
                        //select(By.name("new_group"), contact.getGroup() = selectRandomGroup());
                } else {
                 if (driver.findElements(By.name("new_group")).size() !=0) {
                         throw new Error ("Groupselector exists");
                 }
                }
                //new Select(driver.findElement(By.name("new_group"))).selectByVisibleText(contact.group);
                type(By.name("address2"), contact.getAddress2());
                type(By.name("phone2"), contact.getHome2());
                return this;
        }




        public ContactHelper submitContactCreation() {
                click(By.name("submit"));
                return this;
        }
        public ContactHelper returnToHomePage() {
                click(By.linkText("home page"));
                return this;
        }


        public ContactHelper initContactModification(int index) {
                click(By.xpath("//tr[@name='entry'][" + (index+1) + "]/td[7]/a/img"));
                return this;
        }
                


        public ContactHelper deleteContact() {
                click(By.xpath("//form[2]/input[2]"));
                return this;
                
        }


        public ContactHelper updateContact() {
                click(By.xpath("//input[@value='Update']"));
                return this;
                
        }


}